package com.yikes.srb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.utils.object.BeanUtils;
import com.yikes.srb.leadingModle.form.userIntegral.UserIntegralAddForm;
import com.yikes.srb.leadingModle.form.userIntegral.UserIntegralEditForm;
import com.yikes.srb.leadingModle.form.userIntegral.UserIntegralQuery;
import com.yikes.srb.leadingModle.vo.UserIntegralVO;
import com.yikes.srb.pojo.entity.UserIntegral;
import com.yikes.srb.mapper.UserIntegralMapper;
import com.yikes.srb.service.UserIntegralService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户积分记录表 服务实现类
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserIntegralServiceImpl extends ServiceImpl<UserIntegralMapper, UserIntegral> implements UserIntegralService {

    @Override
    public PageResult<UserIntegralVO> listPage(UserIntegralQuery query) {

        Page<UserIntegral> page = lambdaQuery()
                .eq(ObjectUtil.isNotNull(query.getUserId()), UserIntegral::getUserId, query.getUserId())
                .eq(ObjectUtil.isNotNull(query.getIntegral()), UserIntegral::getIntegral, query.getIntegral())
                .page(Page.of(query.getPageNum(), query.getPageSize()));

        List<UserIntegralVO> bean = BeanUtils.toBean(page.getRecords(), UserIntegralVO.class);
        return PageResult.build(page.getTotal(), page.getPages(), bean);

    }

    @Override
    public UserIntegralVO infoId(Long id) {

        UserIntegral userIntegral = getById(id);
        UserIntegralVO bean = BeanUtils.toBean(userIntegral, UserIntegralVO.class);
        return bean;
    }

    @Override
    public void create(UserIntegralAddForm form) {

        UserIntegral bean = BeanUtils.toBean(form, UserIntegral.class);
        save(bean);
    }

    @Override
    public void edit(UserIntegralEditForm form) {

        UserIntegral bean = BeanUtils.toBean(form, UserIntegral.class);
        updateById(bean);
    }

    @Override
    public void del(Long id) {
        baseMapper.deleteById(id);
    }
}
